package com.xiangxiangjd.serviceImpl;

import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.bean.TrUser;
import com.xiangxiangjd.mapper.TrUserMapper;
import com.xiangxiangjd.service.TrUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.bean.result.ResultPage;
import com.xiangxiangjd.controller.model.ApiResultModel;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2021-06-11
 */
@Service
public class TrUserServiceImpl extends ServiceImpl<TrUserMapper, TrUser> implements TrUserService {
    /**
     * 添加TrUser
     *
     * @param trUser 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addTrUser(TrUser trUser) {
        if (this.save(trUser)) {
            return $.success();
        } else {
            return $.error("添加失败");
        }
    }
    /**
     * 编辑跳转页面的回显数据TrUser
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public TrUser selectTrUserByIdTrUser(String id) {
        return this.getById(id);
    }

    /**
     * 编辑TrUser
     *
     * @param trUser 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editTrUser(TrUser trUser) {
        if (this.updateById(trUser)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条TrUser
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel delTrUsers(List ids) {

        int i = baseMapper.deleteBatchIds(ids);

        return $.success(i);

    }

    /**
     * 分页查询TrUser
     *
     * @param page   页码
     * @param limit  查几条
     * @param trUser 前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findTrUserByPage(Integer page, Integer limit, TrUser trUser) {
        Map<String, Object> paramMap = new HashMap<>();

        page= page==null?1:page;
        limit= limit==null?20:limit;

        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("trUser", trUser);

        List<TrUser> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);

        ResultPage<TrUser> resultPage = new ResultPage<>();
        resultPage.setCode(200);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);

        return resultPage;
    }

    @Override
    public TrUser getUser(String username) {
        return baseMapper.getByUser(username);
    }




}
